//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "BatesDoubleExpEngine.h"
using namespace Cephei::QL::Pricingengines::Vanilla;
#include <gen/QL/Models/Equity/BatesDoubleExpModel.h>
#include <gen/QL/Models/Equity/HestonModel.h>
#include <gen/QL/Instruments/TypePayoff.h>
#include <gen/QL/Pricingengines/Vanilla/AnalyticHestonEngine.h>
using namespace Cephei::QL::Models::Equity;
using namespace Cephei::QL::Instruments;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Vanilla::CBatesDoubleExpEngine::CBatesDoubleExpEngine (Cephei::QL::Models::Equity::IBatesDoubleExpModel^ model, Double relTolerance, UInt64 maxEvaluations) : CAnalyticHestonEngine(CBatesDoubleExpEngine::typeid)
{
    CBatesDoubleExpModel^ _Cmodel;
    try
    {
#ifdef HANDLE
        _phBatesDoubleExpEngine = NULL;
#endif
        _Cmodel = safe_cast<CBatesDoubleExpModel^> (model);
        _Cmodel->Lock();
        boost::shared_ptr<QuantLib::BatesDoubleExpModel>& _model = static_cast<boost::shared_ptr<QuantLib::BatesDoubleExpModel>&> (_Cmodel->GetShared ()); 
        QuantLib::Real _relTolerance = (QuantLib::Real)ValueHelper::Convert (relTolerance);
        QuantLib::Size _maxEvaluations = (QuantLib::Size)ValueHelper::Convert (maxEvaluations);
        _ppBatesDoubleExpEngine = new boost::shared_ptr<QuantLib::BatesDoubleExpEngine> (new QuantLib::BatesDoubleExpEngine ( _model,  _relTolerance,  _maxEvaluations ));
        SetAnalyticHestonEngine (boost::dynamic_pointer_cast<QuantLib::AnalyticHestonEngine> (*_ppBatesDoubleExpEngine));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cmodel != nullptr) _Cmodel->Unlock();
    }
}
Cephei::QL::Pricingengines::Vanilla::CBatesDoubleExpEngine::CBatesDoubleExpEngine (Cephei::QL::Models::Equity::IBatesDoubleExpModel^ model, Microsoft::FSharp::Core::FSharpOption<UInt64>^ integrationOrder) : CAnalyticHestonEngine(CBatesDoubleExpEngine::typeid)
{
    CBatesDoubleExpModel^ _Cmodel;
    try
    {
#ifdef HANDLE
        _phBatesDoubleExpEngine = NULL;
#endif
        _Cmodel = safe_cast<CBatesDoubleExpModel^> (model);
        _Cmodel->Lock();
        boost::shared_ptr<QuantLib::BatesDoubleExpModel>& _model = static_cast<boost::shared_ptr<QuantLib::BatesDoubleExpModel>&> (_Cmodel->GetShared ()); 
        QuantLib::Size _integrationOrder = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (integrationOrder) ? (QuantLib::Size)ValueHelper::Convert (integrationOrder->Value) : 144); //4
        _ppBatesDoubleExpEngine = new boost::shared_ptr<QuantLib::BatesDoubleExpEngine> (new QuantLib::BatesDoubleExpEngine ( _model,  _integrationOrder ));
        SetAnalyticHestonEngine (boost::dynamic_pointer_cast<QuantLib::AnalyticHestonEngine> (*_ppBatesDoubleExpEngine));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cmodel != nullptr) _Cmodel->Unlock();
    }
}
Cephei::QL::Pricingengines::Vanilla::CBatesDoubleExpEngine::CBatesDoubleExpEngine (boost::shared_ptr<QuantLib::BatesDoubleExpEngine>& childNative, Object^ owner) : CAnalyticHestonEngine(CBatesDoubleExpEngine::typeid)
{
#ifdef HANDLE
	_phBatesDoubleExpEngine = NULL;
#endif
	_ppBatesDoubleExpEngine = &childNative;
    _ppAnalyticHestonEngine = new boost::shared_ptr<QuantLib::AnalyticHestonEngine> (boost::dynamic_pointer_cast<QuantLib::AnalyticHestonEngine> (*_ppBatesDoubleExpEngine));
}
Cephei::QL::Pricingengines::Vanilla::CBatesDoubleExpEngine::CBatesDoubleExpEngine (QuantLib::BatesDoubleExpEngine& childNative, Object^ owner) : CAnalyticHestonEngine(CBatesDoubleExpEngine::typeid)
{
#ifdef HANDLE
	_phBatesDoubleExpEngine = NULL;
#endif
	_ppBatesDoubleExpEngine = new boost::shared_ptr<QuantLib::BatesDoubleExpEngine> (&childNative);
    _ppAnalyticHestonEngine = new boost::shared_ptr<QuantLib::AnalyticHestonEngine> (boost::dynamic_pointer_cast<QuantLib::AnalyticHestonEngine> (*_ppBatesDoubleExpEngine));
    _BatesDoubleExpEngineOwner = owner;
    _AnalyticHestonEngineOwner = owner;
}

Cephei::QL::Pricingengines::Vanilla::CBatesDoubleExpEngine::CBatesDoubleExpEngine (CBatesDoubleExpEngine^ copy) : CAnalyticHestonEngine(CBatesDoubleExpEngine::typeid)
{
#ifdef HANDLE
	_phBatesDoubleExpEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBatesDoubleExpEngine = new boost::shared_ptr<QuantLib::BatesDoubleExpEngine> (copy->GetShared());
        _ppAnalyticHestonEngine = new boost::shared_ptr<QuantLib::AnalyticHestonEngine> (boost::dynamic_pointer_cast<QuantLib::AnalyticHestonEngine> (*_ppBatesDoubleExpEngine));
    }
}
Cephei::QL::Pricingengines::Vanilla::CBatesDoubleExpEngine::CBatesDoubleExpEngine (System::Type^ t) : CAnalyticHestonEngine(CBatesDoubleExpEngine::typeid)
{
#ifdef HANDLE
	_phBatesDoubleExpEngine = NULL;
#endif
	if (!t->IsSubclassOf(CBatesDoubleExpEngine::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Vanilla::CBatesDoubleExpEngine::CBatesDoubleExpEngine (QuantLib::Handle<QuantLib::BatesDoubleExpEngine>& childNative, Object^ owner)  : CAnalyticHestonEngine(CBatesDoubleExpEngine::typeid)
{
	_phBatesDoubleExpEngine = &childNative;
	_ppBatesDoubleExpEngine = &static_cast<boost::shared_ptr<QuantLib::BatesDoubleExpEngine>>(childNative.currentLink());
    _ppAnalyticHestonEngine = new boost::shared_ptr<QuantLib::AnalyticHestonEngine> (boost::dynamic_pointer_cast<QuantLib::AnalyticHestonEngine> (*_ppBatesDoubleExpEngine));
    _BatesDoubleExpEngineOwner = owner;
}
Cephei::QL::Pricingengines::Vanilla::CBatesDoubleExpEngine::CBatesDoubleExpEngine (QuantLib::Handle<QuantLib::BatesDoubleExpEngine> childNative)  : CAnalyticHestonEngine(CBatesDoubleExpEngine::typeid)
{
	_phBatesDoubleExpEngine = &childNative;
	_ppBatesDoubleExpEngine = &static_cast<boost::shared_ptr<QuantLib::BatesDoubleExpEngine>>(childNative.currentLink());
    _ppAnalyticHestonEngine = new boost::shared_ptr<QuantLib::AnalyticHestonEngine> (boost::dynamic_pointer_cast<QuantLib::AnalyticHestonEngine> (*_ppBatesDoubleExpEngine));
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Vanilla::CBatesDoubleExpEngine::CBatesDoubleExpEngine (QuantLib::BatesDoubleExpEngine childNative)  : CAnalyticHestonEngine(CBatesDoubleExpEngine::typeid)
{
#ifdef HANDLE
	_phBatesDoubleExpEngine = NULL;
#endif
	_ppBatesDoubleExpEngine = new boost::shared_ptr<QuantLib::BatesDoubleExpEngine> (new QuantLib::BatesDoubleExpEngine (childNative));
    _ppAnalyticHestonEngine = new boost::shared_ptr<QuantLib::AnalyticHestonEngine> (boost::dynamic_pointer_cast<QuantLib::AnalyticHestonEngine> (*_ppBatesDoubleExpEngine));
}
#endif

Cephei::QL::Pricingengines::Vanilla::CBatesDoubleExpEngine::~CBatesDoubleExpEngine ()
{
    if (_ppBatesDoubleExpEngine != NULL)
    {
	    delete _ppBatesDoubleExpEngine;
        _ppBatesDoubleExpEngine = NULL;
    }
}
Cephei::QL::Pricingengines::Vanilla::CBatesDoubleExpEngine::!CBatesDoubleExpEngine ()
{
    if (_ppBatesDoubleExpEngine != NULL)
    {
	    delete _ppBatesDoubleExpEngine;
    }
}
QuantLib::BatesDoubleExpEngine& Cephei::QL::Pricingengines::Vanilla::CBatesDoubleExpEngine::GetReference ()
{
    if (_ppBatesDoubleExpEngine == NULL) throw gcnew NativeNullException ();
	return **_ppBatesDoubleExpEngine;
}
boost::shared_ptr<QuantLib::BatesDoubleExpEngine>& Cephei::QL::Pricingengines::Vanilla::CBatesDoubleExpEngine::GetShared ()
{
    if (_ppBatesDoubleExpEngine == NULL) throw gcnew NativeNullException ();
	return *_ppBatesDoubleExpEngine;
}
QuantLib::BatesDoubleExpEngine* Cephei::QL::Pricingengines::Vanilla::CBatesDoubleExpEngine::GetPointer ()
{
    if (_ppBatesDoubleExpEngine == NULL) throw gcnew NativeNullException ();
	return &**_ppBatesDoubleExpEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BatesDoubleExpEngine>& Cephei::QL::Pricingengines::Vanilla::CBatesDoubleExpEngine::GetHandle ()
{
	if (_phBatesDoubleExpEngine == NULL)
	{
		_phBatesDoubleExpEngine = new Handle<QuantLib::BatesDoubleExpEngine> (*_ppBatesDoubleExpEngine);
	}
	return *_phBatesDoubleExpEngine;
}
#endif
bool Cephei::QL::Pricingengines::Vanilla::CBatesDoubleExpEngine::HasNative () 
{
	return (_ppBatesDoubleExpEngine != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::Vanilla::IBatesDoubleExpEngine^ Cephei::QL::Pricingengines::Vanilla::CBatesDoubleExpEngine_Factory::Create (Cephei::QL::Models::Equity::IBatesDoubleExpModel^ model, Double relTolerance, UInt64 maxEvaluations)
{
    return gcnew CBatesDoubleExpEngine ( model,  relTolerance,  maxEvaluations);
}
Cephei::QL::Pricingengines::Vanilla::IBatesDoubleExpEngine^ Cephei::QL::Pricingengines::Vanilla::CBatesDoubleExpEngine_Factory::Create (Cephei::QL::Models::Equity::IBatesDoubleExpModel^ model, Microsoft::FSharp::Core::FSharpOption<UInt64>^ integrationOrder)
{
    return gcnew CBatesDoubleExpEngine ( model,  integrationOrder);
}
